<template>
  <div>
    <el-card style="width: 50%,margin-left:20%,margin-top:10%" >
      <el-form :v-model="user" label-width="80px" style="padding-right: 20px">
        <div style="margin: 15px; text-align: center">
          <el-upload
              class="avatar-uploader"
              action=""
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
          >
            <img v-if="user.avatar" :src="user.avatar" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>
        <el-form-item label="学号" prop="id">
          <el-input v-model="user.id" placeholder="学号" disabled></el-input>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="user.username" placeholder="用户名" disabled></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="user.phone" placeholder="电话"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="user.email" placeholder="邮箱"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input type="textarea" v-model="user.address" placeholder="地址"></el-input>
        </el-form-item>
        <div style="text-align: center; margin-bottom: 20px">
          <el-button type="primary" @click="update">保 存</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import {jwtDecode} from 'jwt-decode'
import {ShowPerson,updataUser} from '@/api/index/admin'
export default {
  data() {
    const decode = jwtDecode(sessionStorage.getItem('token'));
    const str = decode.sub;
    const regex = /(\d+)/;
    const match = str.match(regex);
    const privateID = match && match[1];
    console.log(privateID);

    return {
      user:{
      id: '2021090632',
      username: 'wdx3',
      phone: '13888888888',
      email: '1052063563@qq.com',
      address: '北京市海淀区',
      avatar: 'http://localhost:8081/file/download/1704178189559_[Thumb]640114796587-9d858fef-c579-3a2a-95d7-e34c3dbd1b10.'
    },
      privateID
    }
  },
  mounted() {
    this.load();
  },
  
  methods: {
    load(){
          ShowPerson().then(res=>{
          console.log(res.data)
          this.user=res.data.data.loginUser.user;
      })
      },
    update() {
      // 保存当前的用户信息到数据库
      updataUser(this.user).then(res => {
        if (res.code === '200') {
          // 成功
          this.$message.success('保存成功')
        } else {
          this.$message.error(res.msg)
        }
      })
    },
  }
}
</script>

<style scoped>
/deep/.el-form-item__label {
  font-weight: bold;
}
/deep/.el-upload {
  border-radius: 50%;
}
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  border-radius: 50%;
}
/deep/.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  border-radius: 50%;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
  border-radius: 50%;
}
</style>